{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# vector 的切片或子向量\n",
    "\n",
    "```{rubric} 探索如何在 C++ 中从给定的向量获取切片或子向量。\n",
    "```\n",
    "\n",
    "## 使用 Range 构造\n",
    "\n",
    "核心思想是获取 vector 的起始和结束位置的输入迭代器，并将它们传递给 vector 类的 Range 构造函数以获得子 vector。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <vector>\n",
    "#include <sstream>\n",
    "#include<string>\n",
    "\n",
    "// std::ostringstream oss;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建简单向量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ 1, 2, 3, 4, 2, 2, 4, 6, 5 }"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "std::vector<int> v = { 1, 2, 3, 4, 2, 2, 4, 6, 5 };\n",
    "v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建切片函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "template<typename T>\n",
    "std::vector<T> slice(std::vector<T> const &v, int m, int n)\n",
    "{\n",
    "\tauto first = v.cbegin() + m;\n",
    "\tauto last = v.cbegin() + n + 1;\n",
    "\n",
    "\tstd::vector<T> vec(first, last);\n",
    "\treturn vec;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ 2, 2, 4, 6 }"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "std::vector<int> sub_vec = slice(v, 4, 7);\n",
    "sub_vec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 {func}`std::copy` 函数\n",
    "\n",
    "我们还可以使用标准算法 {func}`std::copy` 将一个向量的一部分复制到另一个向量，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "template<typename T>\n",
    "std::vector<T> slice(std::vector<T> &v, int m, int n)\n",
    "{\n",
    "    std::vector<T> vec(n - m + 1);\n",
    "    std::copy(v.begin() + m, v.begin() + n + 1, vec.begin());\n",
    "    return vec;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在调用 `std::copy` 之前，确保目标向量有足够的空间容纳所有元素。如果不是这样，最好使用 `std::back_insert` 迭代器，它将在指定的向量上调用 {func}`push_back`，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "template<typename T>\n",
    "std::vector<T> slice(std::vector<T> &v, int m, int n)\n",
    "{\n",
    "    std::vector<T> vec;\n",
    "    std::copy(v.begin() + m, v.begin() + n + 1, std::back_inserter(vec));\n",
    "    return vec;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
